package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 14:56:49      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class CompareCount extends Collector {

    private static final Logger logger = Logger.getLogger(CompareCount.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String orgIP = this.getParamMap().get("Original IP");// 原IP
        String orgShell = this.getParamMap().get("Original Shell");//原Shell
        String pusIP = this.getParamMap().get("Destination IP");//目标IP
        String posShell = this.getParamMap().get("Destination Shell");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        int orgCount = 0;
        int posCount = 0;
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(orgIP, orgShell, (int) (this.interval), 0);
            String line;
            while ((line = reader.readLine()) != null) {
                orgCount = Integer.parseInt(line.trim());
            }
            reader.close();
            reader = CommandReader.getReader(pusIP, posShell, (int) (this.interval), 0);
            while ((line = reader.readLine()) != null) {
                posCount = Integer.parseInt(line.trim());
            }
            int temp = Math.abs(posCount - orgCount);
            propValues[0] = ResPropertyValue.createPropValue("" + temp, propMetas[0]);
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (IOException e) {
            logger.error("failed to collect compare count", e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception ex) {
                    logger.error("failed to close BufferedReader", ex);
                }
            }
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "原IP", "原Shell", "目标IP", "目标Shell" };
        return new String[] { "Original IP", "Destination Shell", "Destination IP", "Destination Shell" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1" };
    }
}
